<?php

declare(strict_types=1);
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/27
 * Time: 10:49 AM
 */

namespace App\Entity;

use Illuminate\Support\Facades\DB;

/**
 * 产品的虚拟实体类，没有对应的表
 * Class ProductVEntity
 */
class ProductVEntity extends ABaseEntity
{
    private function __construct($model)
    {
        $this->model = $model;
    }

    /**
     * @return ProductVEntity|null
     */
    public static function findByOrderItem(OrderItemEntity $orderItem)
    {
        $select = "
            SELECT

              t1.id product_id,
              t2.id product_sku_id,
              t1.title product_title,
              t2.title product_sku_title,
              t2.city_id product_sku_city_id,
              t1.order_image order_image

            FROM products t1, product_skus t2
            WHERE
              t1.id={$orderItem->getProductId()} AND t2.id={$orderItem->getProductSkuId()};
        ";
        /** * @var \stdClass[] $model */
        $model = DB::select($select);
        if (!isset($model[0])) {
            return null;
        }

        return new self($model[0]);
    }

    /**
     * @return int
     */
    public function getProductId()
    {
        return $this->model->product_id;
    }

    /**
     * @return int
     */
    public function getProductSkuId()
    {
        return $this->model->product_sku_id;
    }

    /**
     * @return string
     */
    public function getProductTitle()
    {
        return (string) $this->model->product_title;
    }

    /**
     * @return string
     */
    public function getProductSkuTitle()
    {
        return (string) $this->model->product_sku_title;
    }

    /**
     * @return int
     */
    public function getProductSkuCityId()
    {
        return (int) $this->model->product_sku_city_id;
    }
}
